package com.dxy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

@Slf4j
public class Main {

    public static void main(String[] args) throws Exception {
        try {
            log.info("开始生成数据库文档======>");
            Properties prop = ConfigUtil.getProperties();
            String schema = prop.getProperty("create.doc.schema");
            log.info("schema: {}",schema);
            String includePrefix = prop.getProperty("create.doc.table.include.prefix");
            log.info("includeTableNamePrefix: {}",includePrefix);
            String excludePrefix = prop.getProperty("create.doc.table.exclude.prefix");
            log.info("excludeTableNamePrefix: {}",excludePrefix);
            List<String> excludePrefixList = StrUtil.isNotBlank(excludePrefix) ? Arrays.asList(excludePrefix.split(",")) : null;

            SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
            try (SqlSession session = sqlSessionFactory.openSession()) {
                DbMapper mapper = session.getMapper(DbMapper.class);
                List<TableMeta> tableList = mapper.getTableName(schema, includePrefix);
                if (CollUtil.isNotEmpty(tableList)){
                    try (XWPFDocument doc = new XWPFDocument()) {
                        for (TableMeta tableMeta : tableList) {
                            String tableName = tableMeta.getTableName();
                            String tableComment = tableMeta.getTableComment();
                            if (checkIsExclude(excludePrefixList, tableName)){
                                log.info("表{}被排除！", tableName);
                            }else {
                                log.info("生成表{}的文档数据", tableName);
                                List<ColumnMeta> columnList = mapper.getColumnMeta(schema, tableName);
                                PoiUtil.writeTitle(doc, tableComment + "表");
                                PoiUtil.writeTitle(doc, "表名：" + tableName);
                                PoiUtil.writeTable(doc, columnList);
                            }
                        }

                        String fileName = "db-doc-"+ LocalDateTimeUtil.format(LocalDateTime.now(),"yyMMddHHmm") +".docx";
                        try (FileOutputStream out = new FileOutputStream(fileName)) {
                            doc.write(out);
                            log.info("文档已生成: {}", fileName);
                        }
                    }

                }else {
                    log.info("查询表名为空！");
                }
            }
            log.info("数据库文档生成结束！！！");
        }catch (Exception e){
            log.error("生成数据文档发生异常：", e);
        }
    }


    private static boolean checkIsExclude(List<String> excludePrefixList, String tableName){
        if (excludePrefixList == null || excludePrefixList.isEmpty()) {
            return false;
        }
        for (String pre : excludePrefixList) {
            if (tableName.startsWith(pre)){
                return true;
            }
        }
        return false;
    }




}